package tests;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import simulation.AgentManager;
import simulation.Simulation;
import simulation.TrafficInfrastructureManager;
import simulation.Simulation.AgentProvider;
import agents.Agent;
import agents.resource.CrossRoadAgent;
import agents.resource.ResourceAgent;
import agents.resource.RoadAgent;
import domain.Clock;
import domain.CrossRoad;
import domain.Road;
import facade.TrafficInfrastructureFacade;
import gui.MainWindow;

public class TestFeasibilityAnts implements AgentProvider {
	
	public TestFeasibilityAnts(){
		ExperimentSetup2 e= new ExperimentSetup2();
		agentManager = e.getAgents();
		infrastructure = e.getInfrastructure();
	}
	
	public TrafficInfrastructureManager getInfrastructure(){
		return infrastructure;
	}
	
	private AgentManager getAgentManager(){
		return agentManager;
	}
	
	private TrafficInfrastructureManager infrastructure;
	private AgentManager agentManager;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestFeasibilityAnts e = new TestFeasibilityAnts();
		TrafficInfrastructureManager infrastructure = e.getInfrastructure();
		
		TrafficInfrastructureFacade t = new TrafficInfrastructureFacade(infrastructure);
		
		MainWindow mainWindow = new MainWindow(t);
		mainWindow.setVisible(true);
		
		Simulation sim = new Simulation(e);
		while(true) {
			try {
				Thread.sleep(1000);
				sim.doSimulationStep();
			} catch (InterruptedException i) {
				i.printStackTrace();
			}
		}
	}

	public Collection<Agent> getNewAgents() {
		if(Clock.getInstance().getCurrentTime() == 0)
			return new ArrayList<Agent>(getAgentManager().getAllAgents());
		
		if(Clock.getInstance().getCurrentTime()==3) {
			List<Agent> agentsToAdd = new ArrayList<Agent>();
			
			CrossRoad c0 = new CrossRoad(0,3,"c0");
			infrastructure.addCrossRoad(c0);
			
			CrossRoadAgent agentC1 = (CrossRoadAgent) getAgentManager().getResourceAgentOf("c1");
			List<ResourceAgent> ins = new ArrayList<ResourceAgent>();
			ins.add(agentC1);
			
			CrossRoadAgent agentC0 = new CrossRoadAgent(c0,ins, new ArrayList<ResourceAgent>());
					
			getAgentManager().addResourceAgent(agentC0);
			agentsToAdd.add(agentC0);
			return agentsToAdd;
		}
		
		if(Clock.getInstance().getCurrentTime() == 8){
			
			CrossRoad c7 = new CrossRoad(8, 6, "c7");
			infrastructure.addCrossRoad(c7);
			CrossRoadAgent agentC7 = new CrossRoadAgent(c7);
			getAgentManager().addResourceAgent(agentC7);
			
			CrossRoadAgent agentC6 = (CrossRoadAgent) getAgentManager().getResourceAgentOf("c6");
			CrossRoad c6 = agentC6.getTrafficInfrastructure();
			Road road67 = new Road(c7,c6,"road67");
			infrastructure.addRoad(road67);
			RoadAgent agent67 = new RoadAgent(road67, agentC7, agentC6);
			getAgentManager().addResourceAgent(agent67);
			
			CrossRoad c8 = new CrossRoad(9, 6, "c8");
			infrastructure.addCrossRoad(c8);
			
			List<ResourceAgent> outs = new ArrayList<ResourceAgent>();
			outs.add(agentC7);
			
			CrossRoadAgent agentC8 = new CrossRoadAgent(c8,new ArrayList<ResourceAgent>(),outs);
			getAgentManager().addResourceAgent(agentC8);
					
			List<Agent> agentsToAdd = new ArrayList<Agent>();
			agentsToAdd.add(agentC7);
			agentsToAdd.add(agentC8);
			agentsToAdd.add(agent67);
			
			return agentsToAdd;
		}
		return new ArrayList<Agent>();
	}

	public Collection<Agent> getAgentsToRemove() {
		return new ArrayList<Agent>();
	}

	
}
